/**
 * 代码题第二题
 * 基于loadsh完成四个练习
 */

const fp = require("loadsh/fp");
const cars = [
  { name: "Ferrari FF", horsePower: 660, dollar_value: 700000, in_stock: true },
  {
    name: "Spyker C12 Zagato",
    horsePower: 650,
    dollar_value: 648000,
    in_stock: false,
  },
  {
    name: "Jagur XKR-S",
    horsePower: 550,
    dollar_value: 132000,
    in_stock: false,
  },
  { name: "Audi R8", horsePower: 525, dollar_value: 114200, in_stock: false },
  {
    name: "Aston Martin One-77",
    horsePower: 750,
    dollar_value: 185000,
    in_stock: true,
  },
  {
    name: "Pagani Huayra",
    horsePower: 700,
    dollar_value: 1300000,
    in_stock: false,
  },
];

/**
 * 练习1
 */
let isLastInStock = fp.flowRight([fp.curry(fp.prop)('in_stock'),fp.last]);

/**
 * 练习2
 */
let getFirstName = fp.flowRight([
    fp.curry(fp.prop)('name'),
    fp.first
]);

/**
 * 练习3
 */
let _average = function(xs){
    return fp.reduce(fp.add,0,xs)/xs.length;
}
let averageDollarValue = function(cars){
    return fp.flowRight(_average,fp.map(fp.curry(fp.prop)('dollar_value')))(cars);
}

/**
 * 练习4
 */
let _underscore = fp.replace(/\W+/g,'_');
let sanitizeNames = function(cars){
  return fp.map(fp.flowRight(_underscore,fp.toLower,fp.curry(fp.prop)('name')))(cars);
}
console.log(sanitizeNames(cars));